//
//  BHVersionCheckingView.m
//  Behing
//
//  Created by Lj on 04/01/2018.
//  Copyright © 2018 lj. All rights reserved.
//

#import "BHVersionCheckingView.h"

@interface BHVersionCheckingView ()
@property (nonatomic, strong) UIView *subBackView;
@property (nonatomic, strong) UIView *backView;

@end

@implementation BHVersionCheckingView

- (id)init {
    if (self = [super init]) {
        [self creatView];
    }
    return self;
}

- (void)creatView {
    _subBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height)];
    _subBackView.backgroundColor = [UIColor blackColor];
    _subBackView.alpha = 0;
//    [BHSettingUtil addTapGestureInView:_subBackView addTarget:self action:@selector(backClick) withTag:23132];
    self.frame = _subBackView.bounds;
    [self addSubview:_subBackView];
    
    UIView *backView = [[UIView alloc]init];
    backView.center = CGPointMake(App_Frame_Width/2, App_Frame_Height/2);
    backView.bounds = CGRectMake(0, 0, App_Frame_Width - 100,325 * SizeScale);
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.cornerRadius = 3;
    [self addSubview:backView];
    _backView = backView;
    
    [self setUpUI];

}

- (void)setUpUI {
//    CGPoint foremValue = redView.center;
//    CGPoint toValue = CGPointMake(foremValue.x + 30, foremValue.y);
//    //使用CABasicAnimation创建基础动画
//    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
//    anima.fromValue = [NSValue valueWithCGPoint:foremValue];
//    anima.toValue = [NSValue valueWithCGPoint:toValue];
//    anima.duration = 3.f;
//    anima.fillMode = kCAFillModeForwards;
//    anima.removedOnCompletion = NO;
//    anima.repeatCount = CGFLOAT_MAX;
//    [redView.layer addAnimation:anima forKey:@"positionAnimation"];
    
    UIButton *updateButton = [[UIButton alloc]init];
    [updateButton setTitle:@"立即更新" forState:UIControlStateNormal];
    [updateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    updateButton.backgroundColor = Main_Color;
    updateButton.clipsToBounds = YES;
    updateButton.layer.cornerRadius = 3;
    [updateButton addTarget:self action:@selector(updateButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:updateButton];
    [updateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(20);
        make.width.offset(CGRectGetWidth(self.backView.frame) - 40);
        make.height.offset(40);
        make.bottom.equalTo(self.backView.mas_bottom).offset(- 15);
    }];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, - 48 * SizeScale, CGRectGetWidth(self.backView.frame), 167 * SizeScale)];
//    image.backgroundColor = [UIColor redColor];
    image.image = [UIImage imageNamed:@"20180104042144853"];
//    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:image.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(3, 3)];
//    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//    //设置大小
//    maskLayer.frame = image.bounds;
//    //设置图形样子
//    maskLayer.path = maskPath.CGPath;
//    image.layer.mask = maskLayer;
    [self.backView addSubview:image];
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self.backView addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView);
        make.top.equalTo(image.mas_bottom);
        make.width.offset(CGRectGetWidth(self.backView.bounds));
        make.bottom.equalTo(updateButton.mas_top).offset(- 10);
    }];
    
    UIView *contentView = [[UIView alloc]init];
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    
    NSArray *array = @[@"最新版本：v1.2.1",@"新版本大小：16.9M"];
    UILabel *updateLabel = nil;
    for (int i = 0; i < array.count ; i ++) {
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.text = array[i];
//        nameLabel.textColor = Text_Color_9B99A9;
        nameLabel.font = BHFont_Size_12;
        [contentView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (updateLabel) {
                make.top.equalTo(updateLabel.mas_bottom).offset(5);
            }else {
                make.top.equalTo(contentView).offset(15);
            }
            make.left.equalTo(contentView).offset(15);
            make.right.equalTo(contentView.mas_right).offset(- 15);
        }];
        updateLabel = nameLabel;
    }
    
    UILabel *contentLabel = [[UILabel alloc]init];
    contentLabel.text = @"更新内容";
//    contentLabel.textColor = Text_Color_35343D;
    contentLabel.font = BHFont_Size_15;
    [contentView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(updateLabel.mas_bottom).offset(15);
        make.left.equalTo(contentView).offset(15);
        make.right.equalTo(contentView.mas_right).offset(- 15);
    }];
    
    NSArray *contentArr = @[@"1.修复了不可描述的BUG1.修复了不可描述的BUG1.修复了不可描述的BUG1.修复了不可描述的BUG",@"2.增加了新的功能",@"1.修复了不可描述的BUG1.修复了不可描述的BUG1.修复了不可描述的BUG1.修复了不可描述的BUG",@"1.修复了不可描述的BUG"];
    UILabel *content = nil;
    for (int i = 0; i < contentArr.count ; i ++) {
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.text = contentArr[i];
        nameLabel.numberOfLines = 0;
//        nameLabel.textColor = Text_Color_35343D;
        nameLabel.font = BHFont_Size_12;
        [contentView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (content) {
                make.top.equalTo(content.mas_bottom).offset(5);
            }else {
                make.top.equalTo(contentLabel.mas_bottom).offset(5);
            }
            make.left.equalTo(contentView).offset(15);
            make.right.equalTo(contentView.mas_right).offset(- 15);
        }];
        content = nameLabel;
    }
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(content.mas_bottom);
    }];
}

#pragma mark - show
- (UIView *)topView {
    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    return window.subviews[0];
}

- (void)show {
    [UIView animateWithDuration:0.5 animations:^{
        _subBackView.alpha = 0.5;
        _backView.alpha = 1;
    } completion:^(BOOL finished) {
    }];
    [[self topView]addSubview:self];
    [self showTransformAnimation];
}

#pragma mark - Animation
//transform
- (void)showTransformAnimation {
    CAKeyframeAnimation *alertViewShowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    alertViewShowAnimation.duration = 0.3;
    alertViewShowAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.25f, 0.25f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5f, 0.5f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.75f, 0.75f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)]];
    alertViewShowAnimation.keyTimes = @[@0.25f,@0.5f,@0.75f,@1.0f];
    alertViewShowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [_backView.layer addAnimation:alertViewShowAnimation forKey:nil];
}

#pragma mark -按钮
- (void)updateButtonClick:(UIButton *)sender {
    
}

@end
